package com.iengoo.spacewar;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;


/**
 * 地雷
 */
public class Mine {
    private double x;
    private double y;
    private double angle = 0;

    private double  rotationSpeed = 0.02;
    private double speedY = 0.2;
    /**
     * x坐标速度的倍数（相对与y坐标）
     */
    private double multiple;
    /**
     * 地雷所携带的单词
     */
    private String word;
    /**
     * 当前地雷是否被击中
     */
    private boolean wasHit = false;
    /**
     * 被击中的单词中字母的下标位置（默认未击中，位置为-1）
     */
    private int hitLetterIndex = Config.FIRST_HIT_LETTER_INDEX;
    /**
     * 地雷贴图
     */
    private BufferedImage image;
    /**
     * 字母贴贴
     */
    BufferedImage whiteLetter = null;
    BufferedImage orangeLetter = null;

    // 看成一个正方形，只取贴图有效部分（非透明）的宽度和高度
    public static final int SIZE = 19;

    public Mine(String word, Craft craft) throws IOException {
        this.word = word;
        this.image = ImageIO.read(getClass().getResource("resources/mine_bak.png"));
        this.whiteLetter = ImageIO.read(getClass().getResource("resources/deja-vu-12.png"));
        this.orangeLetter = ImageIO.read(getClass().getResource("resources/deja-vu-12-orange.png"));
        x = (int) (Math.random() * (Config.PANEL_WIDTH - 100));
//        x = 0;
        y = 0;

        multiple = (x - craft.getX()) / (y - craft.getY());
    }

    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        paintWord(word, g, (int) (x + SIZE), (int) (y + (SIZE / 2)));
        g2d.rotate(angle, x + (SIZE / 2), y + (SIZE / 2));
        g2d.drawImage(image, (int) x, (int) y, null);
        g2d.rotate(-angle, x + (SIZE / 2), y + (SIZE / 2));
    }


    /***
     * 将单词转换成对应的字母，并将字母对应赢的贴图打印出来
     * @param word
     */
    public void paintWord(String word, Graphics g, int dx1, int dy1) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

//        Graphics2D g2 = orangeLetter.createGraphics();
//        g2.setComposite(AlphaComposite.Src);
//        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//


        char[] chars = word.toCharArray();

        int preLetterXC = dx1;  // 上一个字母在游戏界面中的x坐标的位置,单词的第一个字母的x坐标的位置为外面传入dx1的值
        for (int i = 0; i < chars.length; i++) {
            LC letter = LC.char2LC(chars[i]);
            if (letter != null) {
                if (wasHit) { // 地雷被击中，剩余字符串颜色改为黄色背景，被击中的改为空
                    if (i > hitLetterIndex) {
                        g2d.drawImage(orangeLetter,
                                // 开始坐标            开始坐标 + 字母对应图片的宽度（结束X坐标-开始X坐标）
                                preLetterXC, dy1, preLetterXC + letter.width, dy1 + letter.height,
                                letter.sx1, letter.sy1, letter.sx2, letter.sy2,
                                null);
                    } else {
                        g2d.drawImage(orangeLetter,
                                // 开始坐标            开始坐标 + 字母对应图片的宽度（结束X坐标-开始X坐标）
                                preLetterXC, dy1, preLetterXC + letter.width , dy1 + letter.height,
                                LC.BLANK.sx1, LC.BLANK.sy1, LC.BLANK.sx2, LC.BLANK.sy2,
                                null);
                    }

                } else {
                    g2d.drawImage(whiteLetter,
                            // 开始坐标            开始坐标 + 字母对应图片的宽度（结束X坐标-开始X坐标）
                            preLetterXC, dy1, preLetterXC + letter.width, dy1 + letter.height,
                            letter.sx1, letter.sy1, letter.sx2, letter.sy2,
                            null);
                }


                // 改变上一个字母结束位置
                preLetterXC += letter.width + 1;
            }
        }
    }

    public void move() {
        this.angle += rotationSpeed;
        this.y += speedY;
        this.x += speedY * multiple;

    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public int getHitLetterIndex() {
        return hitLetterIndex;
    }

    public void setHitLetterIndex(int hitLetterIndex) {
        this.hitLetterIndex = hitLetterIndex;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public boolean getWasHit() {
        return wasHit;
    }

    public void setWasHit(boolean wasHit) {
        this.wasHit = wasHit;
    }
}
